package com.qqtech.qquan.mall.controller;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.mall.enums.MallProductSpecStatusDictEnum;
import com.qqtech.qquan.mall.model.MallProduct;
import com.qqtech.qquan.mall.model.MallProductSpec;
import com.qqtech.qquan.mall.model.MallProductSpecQuery;
import com.qqtech.qquan.mall.model.MallProductSpecVo;
import com.qqtech.qquan.mall.model.MallProductVo;
import com.qqtech.qquan.mall.service.MallProductService;
import com.qqtech.qquan.mall.service.MallProductSpecService;
import com.qqtech.qquan.mall.service.MallShopService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/mall/product/spec")
public class MallProductSpecController extends BaseWebController<MallProductSpec, MallProductSpecQuery> {
	@Resource
	private MallProductSpecService mallProductSpecService;
	@Resource
	private MallProductService mallProductService;
	@Resource
	private MallShopService mallShopService;
    
	@Override
	protected BaseService<MallProductSpec> getBaseService() {
		return mallProductSpecService;
	}
	
	
	/**
	 * 商品规格列表
	 * 
	 * @param mallProductSpecQuery
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Result list(@RequestBody MallProductSpecQuery mallProductSpecQuery) {
		Result result = new Result();
		List<MallProductSpecVo> list = mallProductSpecService.queryListByProductId(mallProductSpecQuery.getProductId());
		result.put("productSpecList", list);
		return result;
	}
	
	/**
	 * 加载商品和规格--用于编辑
	 * @param mallProductSpecQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/detail4editSpec")
	@ResponseBody
	public Result detail4editSpec(@RequestBody MallProductSpecQuery mallProductSpecQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		Integer productId = mallProductSpecQuery.getProductId();
		if (productId == null || productId == 0) {
			result.addErr("商品id为空！");
			return result;
		}
		Integer specId = mallProductSpecQuery.getId();
		if (specId == null || specId == 0) {
			result.addErr("商品规格id为空！");
			return result;
		}
		MallProductVo mallProduct = mallProductService.getById(productId);
		if (mallProduct == null) {
			result.addErr("找不到该商品！");
			return result;
		}
		MallProductSpec productSpec = mallProductSpecService.getById(specId);
		if (productSpec == null) {
			result.addErr("找不到该商品规格！");
			return result;
		}
		Integer shopId = mallProduct.getShopId();//店铺id
		if(!mallShopService.isShopOwner(curUserId,shopId)){
			result.addErr("你不是店主,不能操作！");
			return result;
		}
		result.put("mallProduct", mallProduct);
		result.put("mallProductSpec", productSpec);
		return result;
	}
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/deleteSpec")
	@ResponseBody
	public Result deleteSpec(@RequestBody MallProductSpec mallProductSpec) {
		Result ret = new Result();
		MallProductSpec old = null;
		if (mallProductSpec == null || mallProductSpec.getId() == null) {
			ret.addErr("参数不能为空");
		} else {
			old = mallProductSpecService.getById(mallProductSpec.getId());
			if (old == null) {
				ret.addErr("产品规格不存在");
			}
		}
		List<MallProductSpecVo> specLst = mallProductSpecService.queryListByProductId(old.getProductId());
		if(specLst == null){
			ret.addErr("商品规格不存在");
			return ret;
		}
		if(specLst.size() == 1){
			ret.addErr("目前商品规格仅为一个，无法删除");
			return ret;
		}
		if (ret.checkForm()) {
			Timestamp now = TimeUtil.now();
			MallProductSpec entity = new MallProductSpec();
			entity.setId(mallProductSpec.getId());
			entity.setStatus(MallProductSpecStatusDictEnum.DELETE.getCode());
			entity.setUpdAt(now);
			mallProductSpecService.updateById(entity);
		}
		//取规格价格最小值，更新商品表价格字段
		Integer totalStock = 0;
		BigDecimal minPrice = BigDecimal.ZERO;
		BigDecimal currentPrice = BigDecimal.ZERO;
		for(int i=0; i<specLst.size(); i++){
			MallProductSpecVo specVo = specLst.get(i);
            if(mallProductSpec.getId().equals(specVo.getId())){
				continue;
			}
			currentPrice = specVo.getPrice();
			if(currentPrice == null){
				ret.addErr("商品规格价格为空");
				return ret;
			}
			if(i > 0){
				if(minPrice.compareTo(currentPrice) == 1){//取最小值
					minPrice = currentPrice;
				}
			}else{
				minPrice = currentPrice;
			}
			totalStock += specVo.getStock(); 
		}
		MallProduct updateEntity = new MallProduct();
		updateEntity.setId(old.getProductId());
		updateEntity.setPrice(minPrice);
		updateEntity.setStock(totalStock);
		mallProductService.updateById(updateEntity);
		return ret;
	}
}
